package com.googlecode.puddle;

import com.googlecode.puddle.data.PuddleDbAdapter;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ListPodcasts extends ListActivity {
	private static final String TAG = ListPodcasts.class.getName();

    private static final int ACTIVITY_ADD_PODCAST = 0;
	private static final int ACTIVITY_VIEW_PODCAST = 1;
    
	private static final int ADD_PODCAST_ID = Menu.FIRST;
	private static final int EXIT_ID = Menu.FIRST + 1;
	
	private PuddleDbAdapter podcastDb;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "Setting up List Podcasts activity");
		
		podcastDb = new PuddleDbAdapter(this);
		podcastDb.open();
		
		fillData();
	}

	private void fillData() {
		Log.d(TAG, "Filling podcast list with data");
		Cursor podcastsCursor = podcastDb.fetchAllPodcasts();
		startManagingCursor(podcastsCursor);
		
        String[] from = new String[]{PuddleDbAdapter.PODCAST_TITLE, PuddleDbAdapter.PODCAST_LAST_CHECKED, PuddleDbAdapter.PODCAST_LATEST_SHOW_TITLE};
        int[] to = new int[]{R.id.list_podcasts_row_title, R.id.list_podcasts_row_last_checked, R.id.list_podcasts_row_latest_show};
        
        SimpleCursorAdapter podcastsAdapter = new SimpleCursorAdapter(this, R.layout.list_podcasts_row, podcastsCursor, from, to);
        setListAdapter(podcastsAdapter);
	}

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	Log.d(TAG, "Opening podcast at position " + position + " with id " + id);
    	Intent i = new Intent(this, ViewPodcast.class);
    	i.putExtra("podcast", id);
    	startActivityForResult(i, ACTIVITY_VIEW_PODCAST);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        Log.d(TAG, "Creating options menu");
        menu.add(0, ADD_PODCAST_ID, 0, "Add Podcast");
        menu.add(0, EXIT_ID, 0,  "Exit");
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        int itemId = item.getItemId();
		Log.d(TAG, "Menu item selected: " + itemId);
        switch(itemId) {
        case ADD_PODCAST_ID:
        	addPodcast();
        	return true;
        case EXIT_ID:
        	finish();
        	return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
    }

	private void addPodcast() {
		Log.d(TAG, "Adding a new podcast");
        Intent i = new Intent(this, AddPodcast.class);
        startActivityForResult(i, ACTIVITY_ADD_PODCAST);
	}
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, 
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Log.d(TAG, "Child activity ended, repopulating data");
        fillData();
    }
}